$(function (){
    $('#keyword').on('keyup',function (){
        var keyword=$(this).val();
        //中文模式输入字符会有两次成功输出，待解决
        console.log(keyword);
        if(keyword !== ''&&keyword !== null)
        {
            $.ajax({
                type   :'GET',
                url    :'productAuto?keyword='+keyword,
                success :function(data){
                    console.log(data);
                    var productListHTML = '';
                    for(var i = 0;i < data.length;i++){
                        productListHTML += '<li class="productAutoItem" data-productid="';
                        productListHTML += data[i].productId;
                        productListHTML += '">';
                        productListHTML += data[i].categoryId;
                        productListHTML += ': '
                        productListHTML += data[i].name;
                        productListHTML += '</li>';
                    }
                    $('#productAutoList').html(productListHTML);
                    $('#productAutoComplete').show();
                },
                error  :function (errorMsg){
                    console.log(errorMsg);
                }
            })
        }else{
            $('#productAutoComplete').hide();
        }
    })

    // $('.productAutoItem').on('click',function (){
    //
    // })
    $('#productAutoComplete').on('click','.productAutoItem',function(){
        var productId = $(this).data('productid');
        $('#productAutoComplete').hide();
        $('#keyword').val('');
        window.location.href = 'http://localhost:8080/project_store_Web_exploded/productForm?productId=' + productId;
    });

    $('.nav-search').on('mouseleave',function(){
        $('#productAutoComplete').hide();
        //要不要把搜索框清空？
    });
});
